#include "tracvalidationwidget.h"
#include "ui_tracvalidationwidget.h"

#include "tracsettings.h"

#include <coreplugin/icore.h>

#include <QtCore/QUrl>
#include <QtCore/QSettings>

namespace Trac {
namespace Internal {

TracValidationWidget::TracValidationWidget(QWidget *parent):
    QWidget(parent),
    ui(new Ui::TracValidationWidget),
    m_valid(false)
{
    ui->setupUi(this);
    ui->validationResultLabel->hide();
    connect(ui->anonymousCheckBox, SIGNAL(stateChanged(int)), this, SLOT(anonymousCheckBoxChanged(int)));
    connect(ui->usernameLineEdit, SIGNAL(textChanged(QString)), this, SLOT(checkValid()));
    connect(ui->passwordLineEdit, SIGNAL(textChanged(QString)), this, SLOT(checkValid()));
    connect(ui->hostNameComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(checkValid()));
    connect(ui->hostNameComboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(hostChanged()));

    QSettings *settings = Core::ICore::instance()->settings();
    QString servers = settings->value("Projects/ProjectsServers").toString();
    if (servers.length() == 0) {
        servers = "http://projects.forum.nokia.com/, http://collab.symbian.nokia.com/", "http://projects.developer.nokia.com";
        settings->setValue("Projects/ProjectsServers", servers);
    }
    bool gotFixedUrl = false;
    foreach (QString server, servers.split(",", QString::SkipEmptyParts))
    {
        if (server.trimmed().startsWith("http://projects.forum.nokia.com")) {
            gotFixedUrl = true;
            ui->hostNameComboBox->addItem("http://projects.developer.nokia.com");
        }
        ui->hostNameComboBox->addItem(server.trimmed());
    }

    changeHostname(ui->hostNameComboBox->currentText());
    ui->validatePushButton->setVisible(false);
}

TracValidationWidget::~TracValidationWidget()
{
    delete ui;
}

bool TracValidationWidget::isValid() const
{
    return m_valid;
}

void TracValidationWidget::initialize()
{
}

void TracValidationWidget::anonymousCheckBoxChanged(int state)
{
    if (state == Qt::Checked) {
        ui->validationResultLabel->setVisible(false);

        ui->usernameLineEdit->clear();
        ui->passwordLineEdit->clear();

        TracSettings::instance()->setUsername("anonymous");
        TracSettings::instance()->setPassword("anonymous");
    }
    else if (state == Qt::Unchecked) {
        TracSettings::instance()->setUsername("");
        TracSettings::instance()->setPassword("");
    }

    ui->usernameLineEdit->setEnabled(!ui->anonymousCheckBox->isChecked());
    ui->passwordLineEdit->setEnabled(!ui->anonymousCheckBox->isChecked());

    m_valid = ui->anonymousCheckBox->isChecked() && ui->hostNameComboBox->currentIndex() >= 0;

    if (ui->hostNameComboBox->currentIndex() >= 0)
        changeHostname(ui->hostNameComboBox->currentText());

    emit validChanged(m_valid);
}

void TracValidationWidget::checkValid()
{
    // Check that we are either anonymous or the user has entered both username and password.
    // Also make sure a host is selected.
    m_valid = ((!ui->usernameLineEdit->text().isEmpty() && !ui->passwordLineEdit->text().isEmpty())
               || ui->anonymousCheckBox->isChecked()) && ui->hostNameComboBox->currentIndex() >= 0;
    if (m_valid) {
        TracSettings::instance()->setUsername(ui->usernameLineEdit->text());
        TracSettings::instance()->setPassword(ui->passwordLineEdit->text());
    }
    emit validChanged(m_valid);
}

void TracValidationWidget::hostChanged()
{
    if (ui->hostNameComboBox->currentIndex() >= 0)
        changeHostname(ui->hostNameComboBox->currentText());
}

void TracValidationWidget::changeHostname(const QString &hostname)
{

    QString s = hostname;
    if (!s.endsWith("/rpc")) {
        if (s.endsWith("/"))
            s.truncate(s.length() - 1);
        if (ui->anonymousCheckBox->isChecked())
            s.append("/home/rpc");
        else
            s.append("/home/login/rpc");
    }
    TracSettings::instance()->setHostName(s);
}

} // namespace Internal
} // namespace Trac


